package com.example.demo.controller;

import com.example.demo.model.User;
import com.example.demo.security.jwt.JWTUtils;
import com.example.demo.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class UserController {
    @Autowired
    private JWTUtils jwtUtils;
    @Autowired
    UserService userService;
    private Logger logger= LoggerFactory.getLogger(UserController.class);

    @GetMapping("/user")
    @ResponseBody
    public User getUser(HttpServletRequest request)
    {
        String token = request.getHeader("Authorization");
        String username=jwtUtils.getUsername(token);
        User user=userService.findUserByAccount(username);

        logger.info("header-token:"+token);
        if(user==null)
        {
            logger.info("user is null");
        }
        else {
            logger.info("user is "+user.getUsername());
        }
        return  user;
    }

    @PostMapping("/user/logout")
    @ResponseBody
    public User logout(HttpServletRequest request)
    {
        return null;
    }

}
